//
//  JRNetWorkTools.swift
//  swiftWeiBo
//
//  Created by Johnray on 15/9/23.
//  Copyright © 2015年 Johnray. All rights reserved.
//

import UIKit
import AFNetworking

enum NetWorkType:String{
    case GET = "GET"
    case POST = "POST"

}

class JRNetWorkTools: AFHTTPSessionManager {

  typealias JRNetWorkPamars = (success:AnyObject?,error:NSError?)
    
    
 /// 创建单例
    static let shareNetWorkTools:JRNetWorkTools = {
        
        let instans = JRNetWorkTools(baseURL: nil)
        
        instans.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return instans
    }()
    
    //MARK: - 重要参数
    private let appKey = "3945635079"
    private let appSecret = "e51ff4814eb37423bd9772c5b91ad243"
     let httpAddress = "http://www.baidu.com"
    
    var oathurl:NSURL{
        let url = NSURL(string: "https://api.weibo.com/oauth2/authorize?client_id=\(appKey)&redirect_uri=\(httpAddress)")
    
        return url!
    }


    //MARK: - 获取入口token
    func loadUserAccessToken(code:String,finshed:(success:AnyObject?,error:NSError?)->()){
        
        //client_id	true	string	申请应用时分配的AppKey。
//        client_secret	true	string	申请应用时分配的AppSecret。
//        grant_type	true	string	请求的类型，填写authorization_code
//        
//        grant_type为authorization_code时
//        必选	类型及范围	说明
//        code	true	string	调用authorize获得的code值。
//        redirect_uri	true	string	回调地址，需需与注册应用里的回调地址
        let parames = ["client_id":appKey,
                        "client_secret":appSecret,
                        "grant_type":"authorization_code",
                        "code":code,
                        "redirect_uri":httpAddress]
        

        loadData(NetWorkType.POST, urlstring: "https://api.weibo.com/oauth2/access_token", parames: parames) { (success, error) -> () in
            
            if error != nil{
            
                finshed(success: nil, error: error)
                return
            }
            
            finshed(success: success, error: nil)
            
            
        }

    }
    
    /**
    下载方法
    */
    func loadData( type:NetWorkType,urlstring:String,parames:[String:AnyObject],finshed:(success:AnyObject?,error:NSError?)->()){
        
        let success = { (task:NSURLSessionDataTask, result:AnyObject) -> () in
            
            finshed(success: result, error: nil)
            
        }
        
        let failure = { (task:NSURLSessionDataTask, error:NSError) -> () in

        
            finshed(success: nil, error: error)
        }
        
        if type == NetWorkType.GET{
        
           GET(urlstring, parameters: parames, success: success, failure: failure)
        }else{
        
        POST(urlstring, parameters: parames, success: { (NSURLSessionDataTask, success) -> Void in
            
                finshed(success: success, error: nil)
            }, failure: { (NSURLSessionDataTask, error) -> Void in
                
                finshed(success: nil, error: error)
        })
        
        }
        
        
        
    }
    
}
